package com.honeywen.mymvc.leetcode;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @author weiw@opera.com
 * @date 2018/9/9 下午1:12
 */
public class PlusOneTest {

    public static void main(String[] args) {


        int[] arr = plusOne(new int[]{9,9, 9, 9});
        System.out.println(Arrays.toString(arr));
    }

    public static int[] plusOne(int[] digits) {

        // 需要加1
        int plus = 1;
        for (int i = digits.length - 1; i >= 0; i--) {


            int tmp = digits[i] + plus;
            if (tmp >= 10) {

                digits[i] = tmp % 10;
                plus = 1;
            } else {
                digits[i] = tmp;
                plus = 0;
            }

        }
        if (plus == 1) {
            // 需要处理
            digits[0] = digits[0] % 10;
            int[] arr = new int[digits.length + 1];
            System.arraycopy(digits, 0, arr, 1, digits.length);
            arr[0] = 1;
            return arr;
        }


        return digits;


    }
}
